using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WASM.WebSite.Models;
using System.IO;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace WASM.WebSite.Controllers
{
	[Authorize]
	public class BlobController : Controller
	{
		public ActionResult List(Guid storageId)
		{
			ViewData["storageId"] = storageId;
			ViewData.Model = BlobRepository.Create(storageId).ListOfContainers();
			return View();
		}

		public ActionResult Details(Guid storageId, string storageBoxId)
		{
			BlobRepository blobRepository = BlobRepository.Create(storageId);
			ViewData["storageId"] = storageId;
			ViewData["storageBoxId"] = storageBoxId;
			ViewData["isContainerPrivate"] = blobRepository.IsContainerPrivate(storageBoxId);
			ViewData.Model = blobRepository.ListOfBlobs(storageBoxId);
			return View();
		}

		public ActionResult Download(Guid storageId, string storageBoxId, string itemId)
		{
			BlobProperties blobProperties;
			byte[] content = BlobRepository.Create(storageId).LoadBlobContent(storageBoxId, itemId, out blobProperties);

			string contentType = blobProperties.ContentType;
			if (String.IsNullOrEmpty(contentType))
				contentType = "application/octet-stream";
			return File(content, contentType, blobProperties.Name);
		}

		[ActionName("DeleteContainer")]
		public ActionResult Delete(Guid storageId, string storageBoxId)
		{
			BlobRepository.Create(storageId).Delete(storageBoxId);
			return RedirectToAction("List", new { storageId = storageId });
		}

		[ActionName("DeleteBlob")]
		public ActionResult Delete(Guid storageId, string storageBoxId, string itemId)
		{
			BlobRepository.Create(storageId).Delete(storageBoxId, itemId);
			return RedirectToAction("Details", new { storageId = storageId, storageBoxId = storageBoxId });
		}

		public ActionResult Upload(Guid storageId, string storageBoxId)
		{
			foreach (string fileTageName in Request.Files)
			{
				var file = Request.Files[fileTageName];
				if (file.ContentLength > 0)
				{
					Stream fs = file.InputStream;
					byte[] context = new byte[fs.Length];
					fs.Read(context, 0, (int)fs.Length);

					string fileName = Path.GetFileName(file.FileName);
					BlobRepository.Create(storageId).UploadBlobContent(storageBoxId, fileName, context);
				}
			}
			return RedirectToAction("Details", new { storageId = storageId, storageBoxId = storageBoxId });
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create(Guid storageId)
		{
			ViewData["storageId"] = storageId;
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(Guid storageId, string containerName, bool isPrivate)
		{
			ValidateContainer(storageId, containerName);
			if (ModelState.IsValid)
			{
				BlobRepository.Create(storageId).Create(containerName, isPrivate);
				return RedirectToAction("List", new { storageId = storageId });
			}
			else
			{
				return View();
			}
		}

		private void ValidateContainer(Guid storageId, string containerName)
		{
			if (BlobRepository.Create(storageId).IsContainerExist(containerName))
			{
				ModelState.AddModelError("containerName", String.Format("Container Name [{0}] alredy exists in storage.", containerName));
			}
		}
	}
}
